<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_Plugins_LocalizationRoute extends Gio_Core_Plugin 
{
	/**
	 * @var Gio_Core_Plugins_LocalizationRoute
	 */
	private static $_instance = null;
	
	/**
	 * @return Gio_Core_Plugins_LocalizationRoute
	 */
	public static function getInstance()
	{
		if (null == self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	} 
	
	public function __construct()
	{
		
	}
	
	public function run()
	{
		$request = Gio_Core_Request::getInstance();
		$configs = Gio_Core_Config_Xml::getConfig();
		$adminUrlPrefix = $configs->web->admin->url_prefix;
		$currentUri = $request->getRedirectUrl();
		
		$baseUrl = $request->getBaseUrl();
		$webUrl = (Gio_Core_Application::_initInstallChecker()) ? $configs->web->url : $baseUrl;
		$temp = str_replace($baseUrl, '', $webUrl);
		$rewrite = '';
		
		$uri = $request->getRequestUri();
		
		$serverName = $request->getServerName();
		$serverPort = $request->getServerPort();
		$serverPort = ($serverPort == 80) ? '' : ':' . $serverPort;
		$uri = 'http://' . $serverName . $serverPort . $uri;
		$currentUrl = str_replace($webUrl, '', $uri);
		$uri = '/' . rtrim(ltrim($currentUrl, '/'), '/');
		
		/**
		 * We are in the front-end section
		 * 7 is length of /admin/
		 */
		if (!$adminUrlPrefix || substr($uri, 0, strlen($adminUrlPrefix) + 2) != "/$adminUrlPrefix/") {
			$paths    = explode('/', ltrim($uri, '/'));
			$currLang = array_shift($paths);
		}
		/**
		 * We are in the back-end section
		 */
		else {
			$paths    = explode('/', rtrim($uri, '/'));
			$currLang = array_pop($paths);
		}
		
		/**
		 * Add language parameter.
		 * Set the request URI if there is language in URI
		 */
		$languages = isset($configs->localization->languages->list) 
						? explode(',', $configs->localization->languages->list)
						: array($configs->localization->languages->default);
		
		if (in_array($currLang, explode(',', (string) $configs->localization->languages->list))) {
			$request->setParam('lang', $currLang);
			$path = implode('/', $paths);
			if ('' == $path) {
				$path = '/';
			}
			$currentUri = str_replace('/' . $currLang, '', $currentUri);
			$request->setRedirectUrl(rtrim($currentUri, '/'));
		} else {
			$lang = ($request->getParam('lang') != null) ? $request->getParam('lang')
														 : (string)$configs->web->language; 				
			$request->setParam('lang', $lang);
		}
	}
}
